package com.wll.ppjoke.utils;

import android.content.ContentValues;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import com.wll.libcommon.utils.AppUtils;

import java.util.UUID;

public class MediaStoreUtils {

    public static ContentValues getVideoContentValues() {
        String fileName = UUID.randomUUID().toString() + ".mp4";
//        String filePath = PathUtils.getExternalDcimPath() + "/" + AppUtils.getAppName() + "/" + fileName;
        String filePath = Environment.DIRECTORY_PICTURES + "/" + AppUtils.getAppName() + "/" + fileName;
        return getContentValues("video/mp4", filePath, fileName);
    }

    public static String getFilePath(boolean isVideo){
        String end = "";
        if (isVideo){
            end = ".mp4";
        }
        end = ".jpg";
        String fileName = UUID.randomUUID().toString() + end;
//        String filePath = PathUtils.getExternalDcimPath() + "/" + AppUtils.getAppName() + "/" + fileName;
        String filePath = Environment.DIRECTORY_PICTURES + "/" + AppUtils.getAppName() + "/" + fileName;
        return filePath;
    }

    public static ContentValues getImageContentValues() {
        String fileName = UUID.randomUUID().toString() + ".jpg";
//        String filePath = PathUtils.getExternalDcimPath() + "/" + AppUtils.getAppName() + "/" + fileName;
        String filePath = Environment.DIRECTORY_PICTURES + "/" + AppUtils.getAppName() + "/" + fileName;
        return getContentValues("image/jpeg", filePath, fileName);
    }

    public static ContentValues getContentValues(String mimeType, String filePath, String fileName) {
        long timeMillis = System.currentTimeMillis();

        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
        contentValues.put(MediaStore.MediaColumns.TITLE, fileName);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
        contentValues.put(MediaStore.MediaColumns.DATE_ADDED, timeMillis);
        contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, timeMillis);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, filePath);
            contentValues.put(MediaStore.MediaColumns.DATE_TAKEN, timeMillis);
        } else {
            contentValues.put(MediaStore.MediaColumns.DATA, filePath);
        }
        return contentValues;
    }
}
